use std::collections::HashMap;
use tracing::debug;
use crate::http::http_request::HttpRequest;

pub fn cookie_parse(request: &HttpRequest) -> HashMap<String, String> {

    let cookie_string = match request.headers.get("Cookie") {
        Some(cookie_string) => cookie_string.to_string(),
        None => "".to_string(),
    };

    let mut cookie = HashMap::new();

    if !cookie_string.is_empty() && cookie_string.contains("session_id") {
        let cookies = cookie_string.split(";");
        for cookie_item in cookies {
            if let Some((key, value)) = cookie_item.split_once('=') {
                debug!("cookie_item: {key}: {value}");
                cookie.insert(key.trim().to_string(), value.trim().to_string());
            }
        }
    }

    cookie
}